unit ChangeAuthor;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IBSQL;

type
  TfmChangeAuthor = class(TForm)
    Label1: TLabel;
    lblOldAuthor: TLabel;
    edtNewAuthorId: TEdit;
    Label2: TLabel;
    lblNewAuthor: TLabel;
    btnChange: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure edtNewAuthorIdKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnChangeClick(Sender: TObject);
    procedure edtNewAuthorIdChange(Sender: TObject);
  private
    FOldAuthorId: integer;
    procedure SetOldAuthorId(const Value: integer);
    { Private declarations }
  public
    { Public declarations }
    property OldAuthorId: integer write SetOldAuthorId;
  end;


implementation
uses DicDM;
{$R *.DFM}

procedure TfmChangeAuthor.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfmChangeAuthor.FormCreate(Sender: TObject);
begin
  lblNewAuthor.Caption := '';
  edtNewAuthorId.Text := '';
  btnChange.Enabled := False;
end;

procedure TfmChangeAuthor.edtNewAuthorIdKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var q: TIBSQL;
begin
  if Key = VK_RETURN then begin
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := 'SELECT * FROM AUTHORS WHERE ID = :ID';
    q.ParamByName('ID').AsString := edtNewAuthorId.Text;
    q.ExecQuery;
    if not q.Eof then begin
      lblNewAuthor.Caption := q.FieldByName('LAST_NAME').AsString + ' ' + q.FieldByName('FIRST_NAME').AsString;
      btnChange.Enabled := True;
    end
    else begin
      lblNewAuthor.Caption := '';
      btnChange.Enabled := False;
    end;
    q.Transaction.Commit;
    q.Free;
  end;
end;

procedure TfmChangeAuthor.SetOldAuthorId(const Value: integer);
var q: Tibsql;
begin
    FOldAuthorId := Value;
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := 'SELECT * FROM AUTHORS WHERE ID = :ID';
    q.ParamByName('ID').AsInteger := Value;
    q.ExecQuery;
    if not q.Eof then
      lblOldAuthor.Caption := Format('%s %s (ID %d)',
        [q.FieldByName('LAST_NAME').AsString, q.FieldByName('FIRST_NAME').AsString, Value])
    else
      lblOldAuthor.Caption := '';
    q.Transaction.Commit;
    q.Free;

end;

procedure TfmChangeAuthor.btnChangeClick(Sender: TObject);
var q: Tibsql;
begin
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := 'UPDATE AUTHORS_OF_BOOKS SET AUTHOR_ID = :NEW_ID WHERE AUTHOR_ID = :OLD_ID';
    q.ParamByName('OLD_ID').AsInteger := FOldAuthorId;
    q.ParamByName('NEW_ID').AsString := edtNewAuthorId.Text;
    q.ExecQuery;
    q.Transaction.Commit;
    q.Free;
    ModalResult := mrOk;
end;

procedure TfmChangeAuthor.edtNewAuthorIdChange(Sender: TObject);
begin
  btnChange.Enabled := False;
end;

end.
